import java.util.HashMap;

public class MaximumSum {

    public static void main(String[] args) {
        MaximumSum maximumSum = new MaximumSum();
        int[] nums = new int[]{18,43,36,13,7};
        System.out.println(maximumSum.maximumSum(nums));
    }

    public int maximumSum(int[] nums) {
        HashMap<Integer, Integer[]> map = new HashMap<>();
        int ans = -1;
        for (int num : nums) {
            int process = process(num);
            if (map.containsKey(process)) {
                Integer[] temp = map.get(process);
                int min = Math.min(temp[0], temp[1]);
                if (num > min) {
                    if (temp[0] == min) {
                        temp[0] = num;
                    } else {
                        temp[1] = num;
                    }
                }
                ans = Math.max(ans, temp[0] + temp[1]);
            } else {
                Integer[] temp = {num, -1};
                map.put(process, temp);
            }
        }
        return ans;
    }

    public int process(int num) {
        int ans = 0;
        while (num != 0) {
            ans += num % 10;
            num /= 10;
        }
        return ans;
    }
}
